package delete2;

## This file is part of the Simplemail project.
#  Copyright (c) 2008 the Simplemail Team (see authors).
#
#  For the full copyright and license information, please view the COPYING
#  file that was distributed with this source code. You can also view the
#  COPYING file online at http://unixforum.org.ua/misc/COPYING.txt
#
#  @copyright  (c) the authors
#  @author     Dmitriy Shilenko <rtyug@ukr.net>
#  @license    GPLv2 http://unixforum.org.ua/misc/COPYING.txt
#  @package    Panel Mailbox
#  @version    $Id: $

use strict;
use CGI::Carp qw(fatalsToBrowser);
use base 'func';

sub new {
    my $self = shift->SUPER::new(@_);
    $self->dbi_connect if $self->can("dbi_connect");
    return $self;
}

sub _referer {
    my $self = shift;

    my @url = ();
    $self->{se}->{url_real} = '?action=postfix&postfixactive=';

    push @url, $self->{se}->{url};
    push @url, $self->{se}->{url_real};
    push @url, $self->{se}->{url_active};    # method2

    if ( $self->{p}->{domain} ) {
        push @url, '&fDomain=' . $self->{p}->{domain};
    }

    if ( $self->{p}->{domain2} ) {
        push @url, '&domain=' . $self->{p}->{domain2};
    }

    if ( $self->{p}->{username} && $self->{session}->{name} eq 'Admin' ) {
        push @url, '&username=' . $self->{'p'}->{username};
    }

    if ( $self->{p}->{from} ) {
        push @url, '&from=' . $self->{p}->{from};
    }

    my $referer = join( '', @url );
    print $self->{query}->redirect( -uri => $referer, -status => 301 );

    #return $self;
}

sub edit_admin_active {
    my $self = shift;

    $self->{session}->{filet} = 'editeadminactive';
    $self->insert_logsmail if $self->can("insert_logsmail");

    my $sth = $self->{dbh}->prepare(
        'UPDATE users
                         SET active=1-active
                         WHERE id = ? 
                           '
    );
    $sth->execute( $self->{p}->{username} );
    $sth->finish();

    $self->{p}->{domain}      = undef;
    $self->{p}->{username}    = undef;
    $self->{se}->{url_active} = 'listadmin';
    return $self->_referer();

}

sub edit_domain_active {
    my $self = shift;

    $self->{session}->{filet} = 'editedomainactive';
    $self->insert_logsmail if $self->can("insert_logsmail");

    my @sql  = ();
    my @sqlb = ();
    my $clause;

    if ( $self->{session}->{name} ne 'Admin' ) {
        push @sql, $self->{p}->{domain}, $self->{session}->{user};
        push @sqlb, "AND idnane = ?";

    }
    else {
        push @sql, $self->{p}->{domain};
    }

    $clause = join( '', @sqlb );

    my $sth = $self->{dbh}->prepare(
        "UPDATE domain
                         SET active=1-active
                         WHERE domain = ?
                          $clause
                             "
    );
    $sth->execute(@sql);
    $sth->finish();

    $self->{p}->{domain}      = undef;
    $self->{se}->{url_active} = 'listdomain';

    return $self->_referer();

}

sub edite_mail_active {

    my $self = shift;

    $self->{session}->{filet} = 'editemailactive';
    $self->insert_logsmail if $self->can("insert_logsmail");

    my @sql  = ();
    my @sqlb = ();
    my $clause;

    if ( $self->{session}->{name} ne 'Admin' ) {
        push @sql, $self->{p}->{mailbox}, $self->{p}->{domain},
          $self->{session}->{user};
        push @sqlb, "AND t2.idnane = ?";
        $clause = join( '', @sqlb );
    }
    else {
        push @sql, $self->{p}->{mailbox}, $self->{p}->{domain};
    }

    my $sth = $self->{dbh}->prepare(
        "UPDATE mailbox 
                         AS t1, domain AS t2 
                         SET t1.active=1-t1.active 
                         WHERE t1.username=  ?
                         AND t1.domain = ? 
                          $clause
                             "
    );
    $sth->execute(@sql);
    $sth->finish();

    $self->{p}->{domain2}     = $self->{p}->{domain};
    $self->{p}->{domain}      = undef;
    $self->{se}->{url_active} = 'listvirtual';

    return $self->_referer();

}

sub delete_alias {
    my $self = shift;

    $self->{session}->{filet} = 'deletealias';
    $self->insert_logsmail if $self->can("insert_logsmail");

    my @sql = ();
    my $clause;

    my $dd;

    if ( $self->{session}->{name} ne 'Admin' ) {

        my $sth = $self->{dbh}->prepare(
            'SELECT t1.domain,
                                t1.idnane,
                                t1.aliases,
                                t2.address
                         FROM domain AS t1,
                              alias AS t2
                         WHERE t1.domain = ?
                         AND t1.idnane = ?
                         AND t2.address = ?
                         AND t1.domain = t2.domain
                         LIMIT 1
                             '
        );
        $sth->execute(
            $self->{p}->{domain},
            $self->{session}->{user},
            $self->{p}->{address}
        );
        $dd = $sth->fetchrow_hashref();
        $sth->finish();

        push @sql, $dd->{domain}, $dd->{address};

    }
    else {
        push @sql, $self->{p}->{domain}, $self->{p}->{address};

    }

    if ( $dd->{aliases} ne '-1' ) {
        $self->{dbh}->do(
            qq{DELETE FROM alias 
                         WHERE domain = ?
                         AND address = ?},
            undef, @sql
        );
    }

    $self->{se}->{url_active} = 'listvirtual';
    $self->{p}->{domain2}     = $self->{p}->{domain};
    $self->{p}->{domain}      = undef;

    return $self->_referer();
}

sub delete_domain {

    my $self = shift;

    $self->{session}->{filet} = 'deletedomain';
    $self->insert_logsmail if $self->can("insert_logsmail");

    my $dd;
    my @sql = ();

    my $clause;

    if ( $self->{session}->{name} ne 'Admin' ) {
        push @sql, $self->{p}->{domain}, $self->{session}->{user};
        $clause = 'domain = ? AND idnane = ?';
    }
    else {
        push @sql, $self->{p}->{domain};
        $clause = 'domain = ?';
    }

    my $sth = $self->{dbh}->prepare(
        "SELECT domain
                           FROM domain
                         WHERE $clause 
                         LIMIT 1
                             "
    );
    $sth->execute(@sql);
    $dd = $sth->fetchrow_hashref();
    $sth->finish();

    my $sth = $self->{dbh}->prepare(
        'SELECT username,
                                  domain   
                         FROM mailbox
                         WHERE domain = ?
                             '
    );

    $sth->execute( $dd->{domain} );
    my $mailbox_del = $sth->fetchall_arrayref();
    $sth->finish();

    foreach $_ ( 0 .. $#{$mailbox_del} ) {
        $self->{dbh}->do(
            qq{INSERT INTO mailbox_delete
          (domain, mailbox,created)
          VALUES (?,?,DATETIME("NOW"))},
            undef, $dd->{domain}, $mailbox_del->[$_]->[0]
        );
    }

    $self->{dbh}->do(
        qq{INSERT INTO domain_delete
              (domain,created)
              VALUES (?,DATETIME("NOW"))},
        undef, $dd->{domain}
    );

    $self->{dbh}->do(
        qq{DELETE FROM mailbox 
                     WHERE domain = ?},
        undef, $dd->{domain}
    );

    $self->{dbh}->do(
        qq{DELETE FROM alias
                         WHERE domain = ?},
        undef, $dd->{domain}
    );

    $self->{dbh}->do(
        qq{DELETE FROM domain
                         WHERE domain = ? },
        undef, $dd->{domain}
    );

    $self->{se}->{url_active} = 'listdomain';
    return $self->_referer();
}

sub delete_mailbox {
    my $self = shift;

    $self->{session}->{filet} = 'deletemailbox';
    $self->insert_logsmail if $self->can("insert_logsmail");

    my @sql = ();
    my $clause;

    if ( $self->{session}->{name} ne 'Admin' ) {
        push @sql, $self->{p}->{domain}, $self->{p}->{delete},
          $self->{session}->{user};
        $clause = 't2.domain = ? AND t2.username = ? AND t1.idnane =?';

    }

    else {
        push @sql, $self->{p}->{domain}, $self->{p}->{delete};
        $clause = 't2.domain = ? AND t2.username = ?';

    }

    my $sth = $self->{dbh}->prepare(
        "SELECT t1.created,
                                t2.domain,
                                t2.username
                                
                         FROM domain AS t1,
                              mailbox AS t2
                              
                         WHERE  $clause
                         LIMIT 1
                             "
    );
    $sth->execute(@sql);
    my $dd = $sth->fetchrow_hashref();
    $sth->finish();

    if ( defined( $dd->{username} ) ) {

        $self->{dbh}->do(
            qq{INSERT INTO mailbox_delete
              (domain, mailbox, created)
              VALUES (?,?,DATETIME("NOW"))},
            undef, $dd->{domain}, $dd->{username}
        );

        $self->{dbh}->do(
            qq{DELETE FROM mailbox 
                     WHERE username = ?},
            undef, $dd->{username}
        );

        $self->{dbh}->do(
            qq{DELETE FROM alias
                         WHERE address = ?},
            undef, $dd->{username}
        );
    }

    $self->{p}->{domain2} = $self->{p}->{domain};
    $self->{p}->{domain}  = undef;

    $self->{se}->{url_active} = 'listvirtual';
    return $self->_referer();
}

sub delete_admin {

    my $self = shift;

    $self->{session}->{filet} = 'deleteadmin';
    $self->insert_logsmail if $self->can("insert_logsmail");

    my $sth = $self->{dbh}->prepare(
        'SELECT domain 
                         FROM domain
                         WHERE idnane = ?
                             '
    );

    $sth->execute( $self->{p}->{admin} );
    my $domain_del = $sth->fetchall_arrayref();
    $sth->finish();

    my $mailbox_del;
    foreach $_ ( 0 .. $#{$domain_del} ) {

        my $sth = $self->{dbh}->prepare(
            'SELECT username,
                                  domain   
                         FROM mailbox
                         WHERE domain = ?
                             '
        );
        $sth->execute( $domain_del->[$_]->[0] );
        push @{$mailbox_del}, $_ while $_ = $sth->fetchrow_hashref();
        $sth->finish();
    }

    foreach $_ ( @{$mailbox_del} ) {
        $self->{dbh}->do(
            qq{INSERT INTO mailbox_delete
              (domain, mailbox,created)
              VALUES (?,?,DATETIME("NOW"))},
            undef, $_->{domain}, $_->{username}
        );
    }

    foreach $_ ( 0 .. $#{$domain_del} ) {
        $self->{dbh}->do( qq{DELETE FROM mailbox WHERE domain = ?},
            undef, $domain_del->[$_]->[0] );
    }

    foreach $_ ( 0 .. $#{$domain_del} ) {
        $self->{dbh}->do(
            qq{INSERT INTO domain_delete (domain,created)
              VALUES (?,DATETIME("NOW"))}, undef, $domain_del->[$_]->[0]
        );
    }

    foreach $_ ( 0 .. $#{$domain_del} ) {
        $self->{dbh}->do( qq{DELETE FROM domain WHERE domain = ?},
            undef, $domain_del->[$_]->[0] );
    }

    foreach $_ ( 0 .. $#{$domain_del} ) {
        $self->{dbh}->do( qq{DELETE FROM alias WHERE domain = ?},
            undef, $domain_del->[$_]->[0] );
    }

    $self->{dbh}->do( qq{DELETE FROM admin_domain WHERE idname = ?},
        undef, $self->{p}->{admin} );

    $self->{dbh}
      ->do( qq{DELETE FROM users WHERE id = ?}, undef, $self->{p}->{admin} );

    $self->{p}->{username}    = undef;
    $self->{p}->{domain}      = undef;
    $self->{se}->{url_active} = 'listadmin';
    return $self->_referer();
}

sub DESTROY {
    my $self = shift;
    $self->SUPER::DESTROY if $self->can("SUPER::DESTROY");
}

1;
